<?php


namespace app\common\controller;

use app\admin\model\SystemLog;
use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use custom\Redis;
use custom\Token;
use think\App;
use think\Exception;
use think\facade\Request;
use think\Log;


class AdminController extends BaseController {

    protected $headersFields = ['adcode', 'token'];
    /**
     * 宿主控制器
     * @var string
     */
    protected $contrAsName = '';

    /**
     * 不需要验证的接口单独设置
     * @var array
     */
    protected $notNeedAuthAction = [];
    /**
     * 权限配置
     * 1查看权限、2管理权限、4导入权限、
     * @var array
     */
    protected $levelConfig = [
        1 => [],
        2 => [],
        4 => [],
        8 => []
    ];

    public function initialize(App $app = null) {
        parent::initialize($app);
    }

    /**
     * 权限验证
     */
    protected function auth() {
        if (!in_array('*', $this->notNeedToken) && !in_array(strtolower($this->request->action()), $this->notNeedToken)) {
            if (empty($this->headers['token'])) {
                $this->resErr('请登录访问', RespCode::TOKEN_EMPTY);
            }
            $Token = new Token();
            if (!$this->uInfo = $Token->verifyToken($this->headers['token'], '', $Token::ADMIN_TOKEN, RedisKey::ADMIN_TOKEN_HASH)) {
                $this->resErr();
            }
            if ($this->uInfo['status'] == 0) {
                $redisModel = Redis::getInstance();
                $redisModel->hdel(RedisKey::ADMIN_TOKEN_HASH . '_' . Token::ADMIN_TOKEN, $this->uInfo['id']);
                $this->resErr('账号已被禁用', RespCode::TOKEN_OFF_SITE);
            }
            $level = $this->uInfo['level'];//登录用户拥有的权限
            //普通角色
            if ($level != '*') {
                $levelConfig = $this->levelConfig;//当前访问的控制权限分类
                $controller = strtolower($this->request->controller());
                $action = strtolower($this->request->action());
                $this->contrAsName != '' && $controller = $this->contrAsName;
                $isAuth = !empty($this->notNeedAuthAction) && in_array($action, $this->notNeedAuthAction);
                if (!$isAuth) {
                    if (!isset($level[$controller])) {
                        $this->resErr('权限不足', RespCode::PARAMS_ERR);
                    }
                    foreach ($level[$controller] as $k => $v) {
                        foreach ($levelConfig[$level[$controller][$k]] as $k1 => $v2) {
                            $merge[] = $v2;
                        }
                    }
                    if (!in_array($action, $merge)) {
                        $this->resErr('权限不足', RespCode::PARAMS_ERR);
                    }
                }
            }
            //验证重复提交

            if (in_array(strtolower($this->request->action()), $this->resubmitLock)) {
                if (Request::isPost()) {
                    $this->resubmitLock($this->uInfo['id'], strtolower($this->request->action()), 1);
                }
            }
        }
    }

    /**
     * 重复提交的锁
     * @param $uid
     * @param $action
     */
    protected function resubmitLock($uid, $action, $ex) {
        $key = $uid . "_{$action}";
        if (!$this->resubmitredis($key, $ex)) {
            $this->resErr('请求太频繁', RespCode::DEFAULT_ERR);
        }
    }

    public function resubmitredis($key, $expire) {
        $redisModel = Redis::getInstance();
        $key = "admin_resubmitLock_$key";
        return $redisModel->set($key, 1, ['nx', 'ex' => $expire]);
    }

    protected function checkParams($fields) {
        parent::checkParams($fields);
        $adcode = $this->headers['adcode'];
        !empty($adcode) && empty($this->params['city_code']) && $this->params['city_code'] = $adcode;
        if (!empty($this->uInfo)) {
            empty($this->params['user_id']) && $this->params['user_id'] = $this->uInfo['id'];
        }
        return $this->params;
    }

}
